package com.ly.demo.fileupload;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @author liuyang
 * @des
 * @Version 1.0
 * @Date: 08/07/2021 09:48
 */
@RestController
@RequestMapping("/file/")
@Api("上传文件")
public class FileUploadController {

    @PostMapping("test")
    @ApiOperation("测试")
    public String test(String message) {

        return message;
    }

    @PostMapping("upload")
    @ApiOperation("上传文件")
    public String upload(@RequestParam("file") MultipartFile file) throws Exception {
        String path = new File("").getAbsolutePath() + "/";
        String originalFilename = file.getOriginalFilename();
        String filePath = path + originalFilename;

        int begin = file.getOriginalFilename().indexOf(".");
        int last = file.getOriginalFilename().length();
        String fileType = file.getOriginalFilename().substring(begin, last);
        String fileName = file.getOriginalFilename().substring(0, begin);
        String base64 = encodeBase64File(file, filePath);
        String newFileName = filePath;
        String base64txtFile = fileName + ".txt";
        decoderBase64File(base64, newFileName);
        toFile(base64, base64txtFile);
        return "ok";
    }

    /**
     * 将文件转成base64 字符串
     *
     * @param multipartFile
     * @param filePath
     * @throws Exception
     */

    public static String encodeBase64File(MultipartFile multipartFile, String filePath) throws Exception {

        File file = new File(filePath);
        multipartFile.transferTo(file);

        FileInputStream inputFile = new FileInputStream(file);
        byte[] buffer = new byte[(int)file.length()];
        inputFile.read(buffer);
        inputFile.close();

        //上传完成后，删除文件
        if (file.exists()) {
            file.delete();
        }

        return new BASE64Encoder().encode(buffer);

    }

    /**
     * 将base64字符解码保存文件
     *
     * @param base64Code
     * @param targetPath
     * @throws Exception
     */

    public static void decoderBase64File(String base64Code, String targetPath) throws Exception {
        byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
        FileOutputStream out = new FileOutputStream(targetPath);
        out.write(buffer);
        out.close();

    }

    /**
     * 将base64字符保存文本文件
     *
     * @param base64Code
     * @param targetPath
     * @throws Exception
     */

    public static void toFile(String base64Code, String targetPath) throws Exception {
        byte[] buffer = base64Code.getBytes();
        FileOutputStream out = new FileOutputStream(targetPath);
        out.write(buffer);
        out.close();
    }

}
